<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import {
  fetchScheduleJobLogList,
  deleteScheduleJobLog,
  cleanScheduleJobLog,
  exportScheduleJobLog
} from '@/api/tool/task/log'

const tableData = ref([])
const loading = ref(false)
const total = ref(0)

const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  jobName: '',
  jobGroup: '',
  status: ''
})

// 获取日志列表
const getList = async () => {
  loading.value = true
  try {
    const res = await fetchScheduleJobLogList(queryParams.value)
    if (res.data) {
      tableData.value = res.data.list
      total.value = res.data.total
    }
  } catch (error) {
    console.error('获取日志列表失败:', error)
    // 如果是mock环境，使用模拟数据
    if (import.meta.env.VITE_USE_MOCK === 'true') {
      tableData.value = [
        {
          id: 1,
          jobName: '示例任务1',
          jobGroup: '系统任务',
          invokeTarget: 'task.SampleTask.execute()',
          status: '0',
          createTime: '2024-03-16 10:00:00'
        },
        // 可以添加更多模拟数据...
      ]
      total.value = tableData.value.length
    }
  } finally {
    loading.value = false
  }
}

// 删除日志
const handleDelete = async (id) => {
  try {
    await ElMessageBox.confirm('确认要删除该日志吗？', '警告', {
      type: 'warning'
    })
    await deleteScheduleJobLog(id)
    ElMessage.success('删除成功')
    getList()
  } catch (error) {
    console.error('删除失败:', error)
  }
}

// 清空日志
const handleClean = async () => {
  try {
    await ElMessageBox.confirm('确认要清空所有日志吗？', '警告', {
      type: 'warning'
    })
    await cleanScheduleJobLog()
    ElMessage.success('清空成功')
    getList()
  } catch (error) {
    console.error('清空失败:', error)
  }
}

// 导出日志
const handleExport = async () => {
  try {
    await exportScheduleJobLog(queryParams.value)
    ElMessage.success('导出成功')
  } catch (error) {
    console.error('导出失败:', error)
  }
}

onMounted(() => {
  getList()
})
</script>

<template>
  <div class="log-container">
    <div class="search-bar">
      <el-form :model="queryParams" inline>
        <el-form-item label="任务名称">
          <el-input v-model="queryParams.jobName" placeholder="请输入任务名称" clearable />
        </el-form-item>
        <el-form-item label="任务分组">
          <el-input v-model="queryParams.jobGroup" placeholder="请输入任务分组" clearable />
        </el-form-item>
        <el-form-item label="执行状态">
          <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
            <el-option label="成功" value="0" />
            <el-option label="失败" value="1" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getList">查询</el-button>
          <el-button @click="queryParams = { pageNum: 1, pageSize: 10 }">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <div class="toolbar">
      <el-button type="danger" @click="handleClean">清空</el-button>
      <el-button type="warning" @click="handleExport">导出</el-button>
    </div>

    <el-table
      v-loading="loading"
      :data="tableData"
      border
      style="width: 100%"
    >
      <el-table-column prop="jobName" label="任务名称" />
      <el-table-column prop="jobGroup" label="任务分组" />
      <el-table-column prop="invokeTarget" label="调用目标字符串" show-overflow-tooltip />
      <el-table-column prop="jobMessage" label="日志信息" show-overflow-tooltip />
      <el-table-column prop="status" label="执行状态">
        <template #default="{ row }">
          <el-tag :type="row.status === '0' ? 'success' : 'danger'">
            {{ row.status === '0' ? '成功' : '失败' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" width="180" />
      <el-table-column label="操作" width="100">
        <template #default="{ row }">
          <el-button
            type="danger"
            link
            @click="handleDelete(row.id)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination">
      <el-pagination
        v-model:current-page="queryParams.pageNum"
        v-model:page-size="queryParams.pageSize"
        :page-sizes="[10, 20, 30, 50]"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="getList"
        @current-change="getList"
      />
    </div>
  </div>
</template>

<style scoped>
.log-container {
  padding: 20px;
}

.search-bar {
  margin-bottom: 20px;
  padding: 20px;
  background: var(--bg-primary);
  border-radius: 4px;
}

.toolbar {
  margin-bottom: 20px;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>
